package com.siemens.industrial.web.mvc.dao.admin;

import com.siemens.industrial.model.admin.Tenant;
import com.siemens.industrial.model.partner.User;
import com.siemens.industrial.web.mvc.service.partner.PartnerUserService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/9/18.
 */
@Service
public class TenantDao {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    PartnerUserService partnerTenantService;
    private final static String typeTag = "{typeTag}";
    private static final String getTenantListQuery = "SELECT * FROM `siemens`.`tenant` order by tenant_name";
    private static final String getOneByIdQuery = "SELECT * FROM `siemens`.`tenant` WHERE `tenant_id`=?";
    private static final String getOneByKeyQuery = "SELECT * FROM `siemens`.`tenant` WHERE `tenant_key` = ?";
    private static final String addTenantQuery = "INSERT INTO `siemens`.`tenant` (tenant_id,tenant_key,tenant_name,password,enabled,address,contact,telephone,manager_email,logo_md5,description) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    private static final String updateTenantQuery = "UPDATE `siemens`.`tenant` SET tenant_key=?,tenant_name=?,password=?,enabled=?,address=?,contact=?,telephone=?,manager_email=?,logo_md5=?,description=? WHERE `tenant_id`=?";
    private static final String initTenantPasswordQuery = "UPDATE `siemens`.`tenant` SET `password`=? WHERE `tenant_id`=?;";
    private static final String findOneByTenantNameQwuery = "SELECT * FROM `siemens`.`tenant` WHERE `tenant_name`=?";
    private static final String findOneCheckQwuery = "SELECT * FROM `siemens`.`tenant` WHERE "+typeTag+"=?";

    public List<Tenant> getTenants() {
        return jdbcTemplate.query(getTenantListQuery, Tenant.getRowMapper());
    }

    public Tenant findOneById(String tenantId) {
        try {
            return jdbcTemplate.queryForObject(getOneByIdQuery, new Object[]{tenantId}, new int[]{Types.VARCHAR}, Tenant.getRowMapper());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public Tenant findOneByKey(String tenantKey) {
        List<Tenant> tenants = jdbcTemplate.query(getOneByKeyQuery, new Object[]{tenantKey}, Tenant.getRowMapper());
        return tenants.size() > 0 ? tenants.get(0) : null;
    }

    public int updateById(Tenant tenant) {
        return jdbcTemplate.update(updateTenantQuery, pss -> {
            pss.setString(1, tenant.getTenantKey());
            pss.setString(2, tenant.getTenantName());
            pss.setString(3, tenant.getPassword());
            pss.setInt(4, tenant.getEnabled());
            pss.setString(5, tenant.getAddress());
            pss.setString(6, tenant.getContact());
            pss.setString(7, tenant.getTelephone());
            pss.setString(8, tenant.getManagerEmail());
            pss.setString(9, tenant.getLogoMd5());
            pss.setString(10, tenant.getDescription());
            pss.setString(11, tenant.getTenantId());

        });
    }

    public int save(Tenant tenant) {
       return jdbcTemplate.update(addTenantQuery, pss -> {
            pss.setString(1, tenant.getTenantId());
            pss.setString(2, tenant.getTenantKey());
            pss.setString(3, tenant.getTenantName());
            pss.setString(4, tenant.getPassword());
            pss.setInt(5, tenant.getEnabled());
            pss.setString(6, tenant.getAddress());
            pss.setString(7, tenant.getContact());
            pss.setString(8, tenant.getTelephone());
            pss.setString(9, tenant.getManagerEmail());
            pss.setString(10, tenant.getLogoMd5());
            pss.setString(11, tenant.getDescription());
        });
    }

    /**
     * 重置密码
     * param:加密过的 password
     */
    @Transactional
    public int initPassword(String pwd, String tenantId) {
        return jdbcTemplate.update(initTenantPasswordQuery, pwd, tenantId);
    }

    /**
     * 创建租户库
     *
     * @param sql
     */
    public void createTenantDatabase(String sql) {
        jdbcTemplate.execute(sql);
    }

    public Tenant getTenantByName(String tenantName) {
        try {
            return jdbcTemplate.queryForObject(findOneByTenantNameQwuery, new Object[]{tenantName}, new int[]{Types.VARCHAR}, Tenant.getRowMapper());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public Tenant checkTenant(String type,String param) {
        try {
            return jdbcTemplate.queryForObject(findOneCheckQwuery.replace(typeTag,type), new Object[]{param}, Tenant.getRowMapper());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
